<template>
  <div class="searchBox">
    <el-card class="box-card">
      <div class="top">
        <div class="top_left">
          <el-icon><Search /></el-icon>
          <span class="search_txt">筛选搜索 </span>
        </div>
        <div class="top_right">
          <el-button type="default">重置</el-button>
          <el-button type="primary" @click="queryRoleList">查询搜索</el-button>
        </div>
      </div>
      <div class="under">
        <span>账号/姓名: </span>
        <el-input
          size="small"
          placeholder="请输入角色名称"
          v-model="queryParams.keyword"
        />
      </div>
    </el-card>
  </div>
  <div class="add">
    <el-card>
      <div>
        <el-icon><Tickets /></el-icon><span class="txt">数据列表</span>
      </div>
      <el-button type="primary" @click="add">添加</el-button>
    </el-card>
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="角色名称" prop="name" />
    <el-table-column label="描述" prop="description" />
    <el-table-column label="用户数" prop="adminCount" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        <span>{{ dateFormat(row.createTime) }}</span></template
      >
    </el-table-column>
    <el-table-column label="是否启用" prop="status">
      <template #default="{ row }">
        <el-switch v-model="row.status" :active-value="1" :inactive-value="0" />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'allocRole'"
          @click="allocMenu(row.id)"
          >分配菜单</el-button
        >
        <el-button
          text
          type="primary"
          v-permission="'allocResource'"
          @click="router.push('/ums/allocResource?id=' + row.id)"
          >分配资源</el-button
        >
        <el-button
          text
          type="primary"
          v-permission="'editRole'"
          @click="editRolesFn(row.id)"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="page">
    <el-config-provider :locale="locale">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryRoleList"
        @current-change="queryRoleList"
      />
    </el-config-provider>
  </div>
  <EditRole :show="showRole" @close="closeRole" :id="rolesId"></EditRole>
</template>
<script setup lang="ts">
import { Search, Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { dateFormat } from "@/utils/index";
import { reactive, ref } from "vue";
import { roleListApi } from "./api";
import EditRole from "./components/editRole.vue";
import { useRouter } from "vue-router";

const router = useRouter();

const userList = ref<RoleList[]>([]);
const locale = zhCn;
const total = ref(0);
// 添加
const showRole = ref(false);
const queryParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
const closeRole = (isReload?: boolean) => {
  showRole.value = false;
  if (isReload) queryRoleList();
};
const add = () => {
  showRole.value = true;
};

// 编辑
const rolesId = ref(0);
const editRolesFn = (id: number) => {
  showRole.value = true;
  rolesId.value = id;
};

// 封装请求列表
const queryRoleList = () => {
  roleListApi(queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};

// 这个函数是实现跳转到分配菜单页面
const allocMenu = (id: number) => {
  router.push("/ums/allocMenu?id=" + id);
};

queryRoleList();
</script>
<style lang="less" scoped>
.searchBox {
  :deep(.el-card) {
    padding: 20px;
  }
  .top {
    position: relative;
    display: flex;
    .top_left {
      .search_txt {
        padding-left: 10px;
        font-size: 14px;
      }
    }
    .top_right {
      position: absolute;
      right: 20px;
    }
  }
  .under {
    padding: 20px 40px 0;
    font-size: 14px;
    :deep(.el-input) {
      height: 32px;
      line-height: 32px;
      width: 210px;
    }
  }
}
.add {
  position: relative;
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .txt {
    position: absolute;
    top: 19px;
    left: 40px;
  }
}

:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
